package com.haois.mp.database.module.offaccounts.service;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import com.haois.mp.database.commom.enums.MpTypeEnum;
import com.haois.mp.database.module.common.daomain.BindState;
import com.haois.mp.database.module.common.daomain.WxUser;
import com.haois.mp.database.module.miniapp.dao.MaUserFollowMapper;
import com.haois.mp.database.module.offaccounts.dao.MpUserFollowMapper;
import com.haois.mp.database.util.jx.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by RuiHaoZ on 2018/2/25 16:53
 */
@Service
public class UserFollowService {
    @Autowired
    private MaUserFollowMapper maUserFollowMapper;
    @Autowired
    private MpUserFollowMapper mpUserFollowMapper;
    @Autowired
    private WxMpService wxMpService;

    private WxUser getWxUser(String openId) {
        WxMpUser wxMpUser = null;
        try {
            wxMpUser = wxMpService.getUserService().userInfo(openId);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return wxMpUser != null ? new WxUser(wxMpUser) : null;
    }

    public void subscribe(String openId) {
        WxUser wxUser = this.getWxUser(openId);
        wxUser = wxUser == null ? new WxUser() : wxUser;
        wxUser.setOpenId(openId);
        this.subscribe(wxUser);
    }

    public void subscribe(WxUser wxUser) {
        wxUser.setCreateTime(DateUtil.getYMdHm());
        mpUserFollowMapper.addWxMpUser(wxUser);
    }

    public void updateUser(String openId, String userId, String remark) {
        WxUser wxUser = mpUserFollowMapper.getByOpenId(openId);
        if (wxUser == null) {
            this.subscribe(openId);
            wxUser = mpUserFollowMapper.getByOpenId(openId);
        }
        wxUser.setUpdateTime(DateUtil.getYMdHm());
        wxUser.setUserId(userId);
        wxUser.setRemark(StringUtils.isBlank(remark) ? "未知" : remark);
        mpUserFollowMapper.updateWxMpUser(wxUser);
    }

    public void syncWxMpUser(WxUser wxUser) {
        mpUserFollowMapper.syncWxMpUser(wxUser);
    }


    public WxUser getByUserId(String userId, String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            return mpUserFollowMapper.getByUserId(userId);
        } else {
            return maUserFollowMapper.getByUserId(userId);
        }
    }

    public List<WxUser> getBySubState(String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            return mpUserFollowMapper.getBySubState("1");
        } else {
            return maUserFollowMapper.getList();
        }
    }

    public WxUser getByOpenId(String openId, String sysType) {
        if (MpTypeEnum.OFFACCOUNTS.getType().equals(sysType)) {
            return mpUserFollowMapper.getByOpenId(openId);
        } else {
            return maUserFollowMapper.getByOpenId(openId);
        }
    }

    public List<BindState> getBindState(String[] memberNum, String sysType) {
        List<BindState> bindStates = new ArrayList<>();
        for (int i = 0; i < memberNum.length; i++) {
            BindState bindState = new BindState(memberNum[i]);
            WxUser wxUser = this.getByUserId(memberNum[i], sysType);
            if (wxUser != null) {
                bindState.setBindState((wxUser.getSubscribe() == null && sysType.equals(MpTypeEnum.MINIAPP.getType())) ? true : wxUser.getSubscribe());
                bindState.setOpenId(wxUser.getOpenId());
                bindState.setCreateTime(wxUser.getCreateTime());
                bindState.setUpdateTime(wxUser.getUpdateTime());
            } else {
                bindState.setBindState(false);
            }
            bindStates.add(bindState);
        }
        return bindStates;
    }

}
